﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.Text;

public partial class BaseData_POPArea : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    DataSet ds = null;
    string AreaIDList = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Init_Data();
            GetData();

        }
    }
    //初始化数据

    private void Init_Data()
    {
        Model.TRANER traner = new Model.TRANER();
        traner = new BLL.TRANER().GetModel(Request.QueryString["code"].ToString());
        Label2.Text = traner.TRANER_NAME;
    }
    //读取数据
    private void GetData()
    {
        ds = new DataSet();
        ds = new BLL.COSTCITY().GetCostCityProvince_List(Request.QueryString["code"].ToString(), Request.QueryString["area"].ToString());
        InitTree(TreeView1.Nodes);
    }
    #region 创建一级树
    private void InitTree(TreeNodeCollection Nds)
    {
        DataView dv = new DataView();
        TreeNode tmpNd;
        dv.Table = ds.Tables[0];
        foreach (DataRowView drv in dv)
        {
            tmpNd = new TreeNode();
            tmpNd.Value = drv["ProID"].ToString().Trim();
            tmpNd.Text = drv["ProName"].ToString();
            tmpNd.ShowCheckBox = true;

            DataView dv2 = new DataView();
            dv2.Table = ds.Tables[2];
            dv2.RowFilter = "CC_FIELD1=" + drv["ProID"].ToString().Trim();
            if (dv2.Count > 0)
            {
                tmpNd.Expand();
            }
            else
            {
                tmpNd.Collapse();
            }
            tmpNd.PopulateOnDemand = false;
            Nds.Add(tmpNd);
            InitTree2(tmpNd);


        }
    }
    #endregion

    #region 创建二级树


    private void InitTree2(TreeNode node)
    {
        DataView dv = new DataView();
        TreeNode tmpNd;
        dv.Table = ds.Tables[1];
        dv.RowFilter = "ProID=" + node.Value;
        foreach (DataRowView drv in dv)
        {
            tmpNd = new TreeNode();
            tmpNd.Value = drv["CityID"].ToString().Trim();
            tmpNd.Text = drv["CityName"].ToString();
            tmpNd.ShowCheckBox = true;

            TreeNode tempNode = new TreeNode();
            tempNode.Text = "货到付款";
            tempNode.Value = "1";
            tempNode.ShowCheckBox = true;
            tmpNd.ChildNodes.Add(tempNode);

            DataView dv2 = new DataView();
            dv2.Table = ds.Tables[2];
            dv2.RowFilter = "CC_CITYCODE=" + drv["CityID"].ToString().Trim();
            if (dv2.Count > 0)
            {
                tmpNd.Checked = true;
                node.Checked = true;
                if (dv2[0]["CC_FIELD2"].ToString().Trim() == "1")
                {
                    tempNode.Checked = true;
                }
            }
            node.ChildNodes.Add(tmpNd);
        }
    }
    #endregion

    #region 取选中结点
    private string GetAllNodeText(TreeNodeCollection tnc)
    {
        foreach (TreeNode node in tnc)
        {
            if (node.Checked && node.ChildNodes.Count == 0)
            {
                AreaIDList = AreaIDList + node.Value + "," + node.Parent.Value + "," + node.Parent.Parent.Value + "|";
            }
            else if (node.Checked && node.ChildNodes[0].Value == "1" && node.ChildNodes[0].Checked == false)
            {
                AreaIDList = AreaIDList + "0" + "," + node.Value + "," + node.Parent.Value + "|";
            }
            else
            {
                GetAllNodeText(node.ChildNodes);
            }
        }
        return AreaIDList;
    }
    #endregion

    #region 保存
    protected void Btn_save_Click(object sender, EventArgs e)
    {
        string trancode = Request.QueryString["code"].ToString();
        string id = Request.QueryString["area"].ToString();
        string AreaIdList = GetAllNodeText(TreeView1.Nodes);
        string[] temp = AreaIdList.Split('|');
        List<Model.COSTCITY> list_costcity = new List<Model.COSTCITY>();
        StringBuilder Sb=new StringBuilder();
        Sb.AppendFormat("DELETE [COSTCITY] WHERE [CC_TRANERCODE]= '{0}'AND [CC_DETAILCODE]='{1}'",trancode,id);
        
        try
        {
            new BLL.Jet().CountRunSql(Sb.ToString());
        }
        catch(Exception ex)
        {
            BLL.Common.Alert(this, ex.ToString());
            return;
        }
        for (int i = 0; i < temp.Length - 1; i++)
        {
            string[] templist = temp[i].Split(',');
            Model.COSTCITY costcity = new Model.COSTCITY();
            costcity.CC_TRANERCODE = trancode;
            costcity.CC_DETAILCODE = id;
            costcity.CC_CITYCODE = templist[1];
            costcity.CC_FIELD1 = templist[2];
            costcity.CC_FIELD2 = templist[0];
            list_costcity.Add(costcity);
        }
        if (list_costcity.Count < 1)
        {
            BLL.Common.Alert(this, "请选择要添加的区域");
            return;
        }
        if (new BLL.COSTCITY().TranCost_InsertMore(list_costcity))
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('保存成功');", true);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('保存失败');", true);
        }
    }
    #endregion
}
